import request from '@/utils/request'
import { ProductType } from '@/type/product'


// 获取产品列表
export function getProductList(params?: { page?: number; size?: number; name?: string }) {
  return request({
    url: '/product/list',
    method: 'get',
    params,
  })
}

// 获取产品详情
export function getProductDetail(id: number) {
  // product_id 是数字类型
  return request({
    url: `/products/${id}`,
    method: 'get',
  })
}

// 创建产品
export function createProduct(data: Omit<ProductType, 'product_id'>) {
  return request({
    url: '/product/create',
    method: 'post',
    data,
  })
}

// 更新产品
export function updateProduct(data: ProductType) {
  return request({
    url: '/product/update',
    method: 'put',
    data,
  })
}

// 删除产品
export function deleteProduct(id: number) {
  // product_id 是数字类型
  return request({
    url: `/product/delete/${id}`,
    method: 'delete',
  })
}

export function getProductsByPageDTO(params: {  tags: string[]; page: number; pageSize: number; 
  key?: string; sortBy?: string
 }) {
  return request({
    url: '/products/page',
    method: 'post',
    data: {
      tags: params.tags,
      pageNo: params.page,
      pageSize: params.pageSize,
    },
  }).then((res) => res.data.data as PaginationResult)
}


export interface PaginationResult {
  total: number
  list: ProductType[]
}

